package com.wsc.wisdomchina.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.javaBean.MyShowItemListBean;

import java.security.PolicySpi;
import java.util.LinkedList;
import java.util.List;

/**
 * author: xw
 * created on: 2017/10/30 12:06
 * description:
 */
public class DragGridViewAdapter extends BaseAdapter {
    private Context context;
    private LinkedList<MyShowItemListBean> homeModuleFourList;
    private int hidePosition = AdapterView.INVALID_POSITION;
    private LayoutInflater layoutInflater;

    public DragGridViewAdapter(Context context, LinkedList<MyShowItemListBean> homeModuleFourList) {
        this.context = context;
        this.homeModuleFourList = homeModuleFourList;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return homeModuleFourList.size();
    }

    @Override
    public MyShowItemListBean getItem(int position) {
        return homeModuleFourList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_fourmodule_head, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        if(position!=hidePosition) {
            convertView.setVisibility(View.VISIBLE);
            initializeViews((MyShowItemListBean) getItem(position), (ViewHolder) convertView.getTag(),position);
        }
        else
            convertView.setVisibility(View.GONE);
        convertView.setId(position);
        return convertView;
    }

    private void initializeViews(MyShowItemListBean object, ViewHolder holder, final int position) {
        //TODO implement

        if(object.getMenuId().equals("-1"))
        {
            holder.iv_add.setVisibility(View.GONE);
            holder.itemHeadName.setText("待添加");
            Glide.with(context).load(R.mipmap.daitianjia)
                    .placeholder(R.mipmap.own_pic_logout)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(holder.itemHeadImg);
        }else {
            MyShowItemListBean.MenuserverBean menuserverBean=object.getMenuserver();
            if(object!=null && menuserverBean!=null)
            {
                Glide.with(context).load(menuserverBean.getIcon())
                        .placeholder(R.mipmap.own_pic_logout)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .into(holder.itemHeadImg);
                holder.itemHeadName.setText(menuserverBean.getName());
            }

            holder.iv_add.setImageResource(R.mipmap.deletereveal);
            holder.iv_add.setVisibility(View.VISIBLE);

            holder.iv_add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnItemDeleteListener.onDeleteClick(position);
                }
            });
        }


    }

    /**
     * 删除按钮的监听接口
     */
    public interface onItemDeleteListener {
        void onDeleteClick(int i);
    }

    private onItemDeleteListener mOnItemDeleteListener;

    public void setOnItemDeleteClickListener(onItemDeleteListener mOnItemDeleteListener) {
        this.mOnItemDeleteListener = mOnItemDeleteListener;
    }


    protected class ViewHolder {
        private ImageView itemHeadImg;
        private TextView itemHeadName;
        private ImageView iv_add;

        public ViewHolder(View view) {
            itemHeadImg = (ImageView) view.findViewById(R.id.item_sida_gongnneg_head_img);
            itemHeadName = (TextView) view.findViewById(R.id.item_sida_gongnneg_head_name);
            iv_add=(ImageView) view.findViewById(R.id.iv_add);
        }
    }
    public void hideView(int pos) {
        hidePosition = pos;
        notifyDataSetChanged();
    }

    public void showHideView() {
        hidePosition = AdapterView.INVALID_POSITION;
        notifyDataSetChanged();
    }

    public void removeView(int pos) {
        homeModuleFourList.remove(pos);
        notifyDataSetChanged();
    }

    //更新拖动时的gridView
    public void swapView(int draggedPos, int destPos) {
        //从前向后拖动，其他item依次前移
        if(draggedPos < destPos) {
            homeModuleFourList.add(destPos+1, getItem(draggedPos));
            homeModuleFourList.remove(draggedPos);
        }
        //从后向前拖动，其他item依次后移
        else if(draggedPos > destPos) {
            homeModuleFourList.add(destPos, getItem(draggedPos));
            homeModuleFourList.remove(draggedPos+1);
        }
        hidePosition = destPos;
        onswapView.onSwapView();
        notifyDataSetChanged();
    }

    /**
     * 删除按钮的监听接口
     */
    public interface OnswapView {
        void onSwapView();
    }

    private OnswapView onswapView;

    public void setOnswapView(OnswapView onswapView) {
        this.onswapView = onswapView;
    }

    public LinkedList<MyShowItemListBean> getHomeModuleFourList() {
        return homeModuleFourList;
    }
}
